package com.example;

import java.security.MessageDigest;
import java.util.zip.CRC32;

import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Encoder;

/**
 * Created by yto on 2019/10/26.
 */
public class Dsas {

    public static final String GBK = "GBK";
    public static final String UTF8 = "UTF-8";

    public static byte[] encryptMD5(byte[] data) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(data);
        return md5.digest();
    }

    public static String encryptBASE64(byte[] md5) throws Exception {
        return new BASE64Encoder().encodeBuffer(md5);
    }

    public static String digest(String data, String sign, String charset)
            throws Exception {
        String t = encryptBASE64(encryptMD5((data + sign).getBytes(charset)));

        if (System.getProperty("line.separator").equals("\n")) {
            String t2 = t.replaceAll("\\n", "\r\n");
            t2 = t2.replaceAll("\r\n", "");
            return t2;
        }
        return t.replaceAll("\r\n", "");
    }

    public static void main(String[] args) throws Exception {

        String stationCode = "20019";
        CRC32 crc32 = new CRC32();
        crc32.update(stationCode.getBytes());
        System.out.println(stationCode.getBytes() + " : " + crc32.getValue());
        long mod = crc32.getValue() % 40;
        System.out.println(mod);
    }
}
